﻿using System.Collections.Generic;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.Common;

namespace T.FAS.Runtime.DataAccess.Engine
{
    public abstract class FilterParser
    {
        /// <summary>
        /// 普通操作符，直接拼接SQL
        /// </summary>
        protected static HashSet<string> NormalOpe = new HashSet<string>
        {
            QueryOperator.Equal,
            QueryOperator.NotEqual,
            QueryOperator.LessThan,
            QueryOperator.GreaterThan,
            QueryOperator.LessEqualThan,
            QueryOperator.GreaterEqualThan,
        };
        /// <summary>
        /// 模糊匹配操作符
        /// </summary>
        public static HashSet<string> LikeOpe = new HashSet<string>
        {
            QueryOperator.Contains,
            QueryOperator.StartsWith,
            QueryOperator.EndsWith,
            QueryOperator.FixedLengthContains,
            QueryOperator.NotContains,
        };
        public Filter Filter { get; set; }
        /// <summary>
        /// 条件表达式
        /// </summary>
        public string ValueCode { get; set; }

        public virtual string ToSQL()
        {
            return ToSQLInternal();
        }

        private string ToSQLInternal()
        {
            if (Filter.Value[0] != null)
                return $"({Filter.Code} {Filter.Condition} {ValueCode})";
            else
            {
                //string为空的情况特殊处理，需要查出空字符串的情况
                if (Filter.DataType.ToLower() == "string")
                {
                    if (Filter.Condition == QueryOperator.Equal)
                    {
                        return $"({Filter.Code} is NULL or {Filter.Code}='')";
                    }
                    else if (Filter.Condition == QueryOperator.NotEqual)
                    {
                        return $"({Filter.Code} is NOT NULL and {Filter.Code}!='')";
                    }
                    else if (Filter.Condition == QueryOperator.Contains)
                    {
                        return $"({Filter.Code} LIKE null)";
                    }
                    else
                    {
                        throw new FasException(DataAccessErrorCode.DataQuery.QueryPlanError, $"字段:{Filter.Code},条件:{Filter.Condition} 不能输入空值");
                    }
                }
                else
                {
                    return $"({Filter.Code} {(Filter.Condition == QueryOperator.Equal ? "is" : "is not")} NULL)";
                }
            }
        }

        protected bool IsNULLValue(out string sql)
        {
            sql = null;
            //特殊处理value为NULL的情况
            if (Filter.Condition != QueryOperator.Between || Filter.Value.Count == 1)
            {
                if (Filter.Value[0] == null)
                {
                    sql = ToSQLInternal();
                    return true;
                }
            }
            return false;
        }
    }
}